package www_news

import (
	"gitee.com/theegg/go-brick/app/model/content/nodes"
	"gitee.com/theegg/go-brick/app/service/app"
	"gitee.com/theegg/go-brick/app/service/help/cbcrypt"
	"gitee.com/theegg/go-brick/app/service/help/cstruct"
	"github.com/gin-gonic/gin"
	"strconv"
)

// @title GetNewsList.go
// @description 获取新闻列表
func GetNewsList(c *gin.Context) {
	// 检验参数
	var validaParams nodes.NodeQueryParams
	if err := c.ShouldBind(&validaParams); err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	data, err := nodes.GetDataList(cstruct.StructToMap(validaParams))
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), data)
		return
	}

	data["news_category"] = nodes.NodeCategory
	app.NewResponse(c).JsonReturn(true, "获取数据成功", data)
	return
}

func GetNewsDetail(c *gin.Context) {
	idStr := c.PostForm("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	item, errCache := nodes.GetItemDetailCacheById(id)
	if errCache != nil {
		app.NewResponse(c).JsonReturn(false, errCache.Error(), nil)
		return
	}

	aesKey := app.GetAesKey()

	data := map[string]interface{}{
		"image_cover_url": app.GetImageStaticUrl(item["image_cover_url"].(string)),
		"title":           cbcrypt.AesEncrypt([]byte(item["title"].(string)), aesKey),
		"content":         cbcrypt.AesEncrypt([]byte(item["content"].(string)), aesKey),
		"author_name":     cbcrypt.AesEncrypt([]byte(item["username"].(string)), aesKey),
		"publish_time":    item["public_time"].(string),
	}

	app.NewResponse(c).JsonReturn(true, "获取数据成功", data)
	return
}
